import express, {Request, Response} from 'express';
import cors from 'cors';
import {connectDB} from './db';  // 导入数据库连接函数
import logger from "./utils/logger";
import {HomeModel} from "./models/homeModel";
import {LinksModel} from "./models/links";
import {migrateLinks} from "./servers/dbs";

// 创建 Express 实例
const app = express();
app.use(express.json({limit: '50mb'}))
app.use(express.urlencoded({limit: '50mb', extended: true}));

// 连接数据库
connectDB().then();

// 使用中间件
app.use(cors());
app.use(express.json());

app.post('/add-data', async (req: Request, res: Response) => {
    const data = req.body;
    logger.info({msg: 'deduplication', data: data});

    const batchSize = 1000; // 每次批量处理的数量
    let totalInsertedLinks: string[] = []; // 存储所有插入的链接

    for (let i = 0; i < data.length; i += batchSize) {
        const batch = data.slice(i, i + batchSize);

        const bulkOps = batch.map((user: any) => ({
            updateOne: {
                filter: {link: user.link},
                update: {
                    $set: user,
                    $setOnInsert: {createdAt: new Date()}
                },
                upsert: true
            }
        }));

        try {
            const bulkResult = await HomeModel.bulkWrite(bulkOps);

            const insertedIds: string[] = Object.values(bulkResult.upsertedIds) as string[];

            // 查询插入的文档链接
            if (insertedIds.length > 0) {
                const links = await HomeModel.find({_id: {$in: insertedIds}})
                    .select('link -_id') // 仅选择 link 字段并排除 _id
                    .lean(); // 使用 lean() 提高查询效率，仅返回 JavaScript 对象而不是 Mongoose 文档

                // 确保正确地获取每个 link 的值
                const linkValues: string[] = links.map(linkObj => linkObj.link as string);

                // 合并结果
                totalInsertedLinks = totalInsertedLinks.concat(linkValues);
            }
        } catch (error) {
            logger.error({msg: '批量写入失败', error: error});
        }
    }

    // 返回所有插入的链接
    res.json({remainingCount: totalInsertedLinks});
});

app.post('/upload-data', async (req: Request, res: Response) => {
    console.log(' 1111 ');
    const data = req.body; // 前端上传的解析后的数据

    // 批量处理的数据大小
    const batchSize = 1000;
    let totalInsertedLinks: string[] = []; // 存储所有插入的链接

    for (let i = 0; i < data.length; i += batchSize) {
        // 取批量的数据
        const batch = data.slice(i, i + batchSize);

        // 创建批量更新操作
        const bulkOps = batch.map((item: { link: string }) => ({
            updateOne: {
                filter: {link: item.link},
                update: {
                    $set: item,
                    $setOnInsert: {createdAt: new Date()},
                },
                upsert: true, // 如果不存在则插入
            },
        }));

        try {
            // 执行批量写入操作
            const bulkResult = await LinksModel.bulkWrite(bulkOps);
            const insertedIds: string[] = Object.values(bulkResult.upsertedIds) as string[];

            // 查询插入的文档链接
            if (insertedIds.length > 0) {
                const links = await LinksModel.find({_id: {$in: insertedIds}})
                    .select('link -_id') // 仅选择 link 字段并排除 _id
                    .lean(); // 使用 lean() 提高查询效率，仅返回 JavaScript 对象而不是 Mongoose 文档

                // 确保正确地获取每个 link 的值
                const linkValues: string[] = links.map(linkObj => linkObj.link as string);

                // 合并结果
                totalInsertedLinks = totalInsertedLinks.concat(linkValues);
            }
        } catch (error) {
            console.error('批量写入失败:', error);
        }
    }

    // 返回所有插入的链接
    res.json({remainingCount: totalInsertedLinks, lengths: totalInsertedLinks.length});
});

const port = process.env.MAINPORT || '';
// 启动服务器
app.listen(port, () => {
    logger.info(`Server running on http://localhost:${port}`);
});
